﻿@*
//------------------------------------------------------------------------------
//  此代码版权声明为全文件覆盖，如有原作者特别声明，会在下方手动补充
//  此代码版权（除特别声明外的代码）归作者本人Diego所有
//  源代码使用协议遵循本仓库的开源协议及附加协议
//  Gitee源代码仓库：https://gitee.com/diego2098/ThingsGateway
//  Github源代码仓库：https://github.com/kimdiego2098/ThingsGateway
//  使用文档：https://diego2098.gitee.io/thingsgateway-docs/
//  QQ群：605534569
//------------------------------------------------------------------------------
*@

@page "/tgconfig/driverdebug"
@namespace ThingsGateway.Web.Page
@using System.Linq.Expressions;
@using BlazorComponent;
@using Furion.DataValidation;
@using Mapster;
@using Masa.Blazor.Presets;
@using System.IO;
@using TouchSocket.Core;
@inject IVariableService VariableService
@attribute [Authorize]
@inject MasaBlazor MasaBlazor
@attribute [RolePermission]
@inherits CultureComponentBase
@inject UserResoures UserResoures
@inject ICollectDeviceService CollectDeviceService
@layout MainLayout
<MRow NoGutters>
    <MCol Md=@(IsShowTreeView?4:0)>
        <MCard Show=IsShowTreeView Style="height: calc(100vh - 200px)">
            <MCardTitle>
                <MTextField Dense Style="max-width:200px;" HideDetails=@("auto") Class="mx-2 my-1" @bind-Value="_searchName" Outlined Label=@typeof(CollectDevice).GetDescription(nameof(CollectDevice.PluginId)) />
            </MCardTitle>
            <MTreeview Style="height: calc(100vh - 320px);overflow-y:auto" Dense TItem="DriverPluginCategory"
                       TKey="long" OpenOnClick ActiveChanged=@((async a=>
                       {
                       if(this._searchModel.PluginId!= a.FirstOrDefault()&&a.Count>0)
                       {
                       this._searchModel.PluginId= a.FirstOrDefault();
                       await ImportVaiable(this._searchModel.PluginId);
                       }
                       }) )
                       Items="DriverPlugins" ItemText="r=>r.Name" ItemChildren="r=>r.Children"
                       Search="@_searchName"
                       Activatable ItemKey=@(r=>r.Id)>
                <LabelContent>
                    <span title=@context.Item.Name>
                        @context.Item.Name
                    </span>
                </LabelContent>
            </MTreeview>
        </MCard>
    </MCol>
    <MCol Md=@(IsShowTreeView?8:12)>
        <MCard Style="height: calc(100vh - 200px)">
            <MCard Class="mb-3 pa-2  text-h6" Elevation="0" Flat  Rounded="false">
                <MRow Align="AlignTypes.Center" Class="mt-1">
                    <MButton Class="mx-1" OnClick=@(async() =>
                             {
                             IsShowTreeView=!IsShowTreeView;
                             }
                             ) Color="primary" Icon>
                        <MIcon>mdi-menu</MIcon>
                    </MButton>
                    <MLabel>
                        @_importRef?.ToString()
                    </MLabel>
                </MRow>
            </MCard>
            @if (_importRender != null)
            {
                @_importRender
            }
        </MCard>

    </MCol>

</MRow>
@code {
    bool IsShowTreeView = true;
    class PluginDebugUIInput
    {
        public string PluginName { get; set; }
        public long PluginId { get; set; }
    }
}

@code {
    string _searchName;
    List<DeviceTree> _deviceGroups = new();
    PluginDebugUIInput _searchModel { get; set; } = new();
    private BootstrapDynamicComponent _importComponent;
    private RenderFragment _importRender;
    private DriverDebugUIBase _importRef;
    List<DriverPluginCategory> DriverPlugins;
    [Inject]
    IDriverPluginService DriverPluginService { get; set; }

    protected override void OnInitialized()
    {
        DriverPlugins = DriverPluginService.GetDriverPluginChildrenList();
        base.OnInitialized();
    }
    async Task ImportVaiable(long driverId)
    {
        var driver = ServiceExtension.GetBackgroundService<CollectDeviceWorker>().GetDebugUI(driverId);
        if (driver == null)
        {
            await PopupService.EnqueueSnackbarAsync("插件未实现调试页面", AlertTypes.Warning);
            return;
        }
        _importComponent = new BootstrapDynamicComponent(driver);
        _importRender = _importComponent.Render(a => _importRef = (DriverDebugUIBase)a);
    }
}